package com.emex.app.stuff.report;

import com.emex.app.shared.data.OrderPosition;
import com.emex.app.shared.data.Orders;
import com.emex.app.shared.data.PlanFact;
import com.emex.app.stuff.provider.CostDataProvider;
import com.emex.app.stuff.provider.OrderDataProvider;
import com.emex.app.stuff.utils.DateUtils;
import com.emex.app.stuff.utils.UIUtils;
import java.awt.Color;
import java.util.Calendar;
import java.util.List;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//com.emex.app.stuff.report//Motivation//EN",
        autostore = false
)
@TopComponent.Description(
        preferredID = "MotivationTopComponent",
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_NEVER
)
@TopComponent.Registration(mode = "editor", openAtStartup = false)
@ActionID(category = "Window", id = "com.emex.app.stuff.report.MotivationTopComponent")
@ActionReference(path = "Menu/Отчетность" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_MotivationAction",
        preferredID = "MotivationTopComponent"
)
@Messages({
    "CTL_MotivationAction=План-факт анализ",
    "CTL_MotivationTopComponent=План-факт анализ",
    "HINT_MotivationTopComponent=План-факт анализ"
})
public final class MotivationTopComponent extends TopComponent {

    private PlanFact planFact;

    public MotivationTopComponent() {
        initComponents();
        setName(Bundle.CTL_MotivationTopComponent());
        setToolTipText(Bundle.HINT_MotivationTopComponent());
        headerLabel.setText("План за " + DateUtils.getMonth(Calendar.getInstance().get(Calendar.MONTH))
                + " " + Calendar.getInstance().get(Calendar.YEAR) + " г.");
        planFact = CostDataProvider.getInstance().getCurrentMonthPlanFact();
        if (null != planFact) {
            buyField.setText(planFact.getGenPurchase().toString());
            sellField.setText(planFact.getGenSell().toString());
            nacenkaField.setText(planFact.getGenNacenka().toString());
            calculateButtonActionPerformed(null);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        headerLabel = new javax.swing.JLabel();
        planPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        buyField = new javax.swing.JTextField();
        sellField = new javax.swing.JTextField();
        nacenkaField = new javax.swing.JTextField();
        factPanel = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        buyCurrentField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        sellCurrentField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        nacenkaCurrentField = new javax.swing.JTextField();
        calculateButton = new javax.swing.JButton();
        factCurrentDayPanel = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        buyCurDayField = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        sellCurDayField = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        nacenkaCurDayField = new javax.swing.JTextField();

        headerLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        headerLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        org.openide.awt.Mnemonics.setLocalizedText(headerLabel, org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.headerLabel.text")); // NOI18N

        planPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.planPanel.border.title"))); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.jLabel1.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.jLabel2.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.jLabel3.text")); // NOI18N

        buyField.setText(org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.buyField.text")); // NOI18N

        sellField.setText(org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.sellField.text")); // NOI18N

        nacenkaField.setText(org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.nacenkaField.text")); // NOI18N

        javax.swing.GroupLayout planPanelLayout = new javax.swing.GroupLayout(planPanel);
        planPanel.setLayout(planPanelLayout);
        planPanelLayout.setHorizontalGroup(
            planPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(planPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(planPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(21, 21, 21)
                .addGroup(planPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(buyField)
                    .addComponent(sellField)
                    .addComponent(nacenkaField, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE))
                .addGap(27, 27, 27))
        );
        planPanelLayout.setVerticalGroup(
            planPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(planPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(planPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(buyField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(planPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(sellField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(planPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(nacenkaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        factPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.factPanel.border.title"))); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.jLabel4.text")); // NOI18N

        buyCurrentField.setText(org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.buyCurrentField.text")); // NOI18N
        buyCurrentField.setEnabled(false);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.jLabel5.text")); // NOI18N

        sellCurrentField.setText(org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.sellCurrentField.text")); // NOI18N
        sellCurrentField.setEnabled(false);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel6, org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.jLabel6.text")); // NOI18N

        nacenkaCurrentField.setText(org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.nacenkaCurrentField.text")); // NOI18N
        nacenkaCurrentField.setEnabled(false);

        javax.swing.GroupLayout factPanelLayout = new javax.swing.GroupLayout(factPanel);
        factPanel.setLayout(factPanelLayout);
        factPanelLayout.setHorizontalGroup(
            factPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(factPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(factPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(21, 21, 21)
                .addGroup(factPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(buyCurrentField)
                    .addComponent(sellCurrentField)
                    .addComponent(nacenkaCurrentField, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE))
                .addGap(27, 27, 27))
        );
        factPanelLayout.setVerticalGroup(
            factPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(factPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(factPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(buyCurrentField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(factPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(sellCurrentField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(factPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(nacenkaCurrentField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        calculateButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/ok.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(calculateButton, org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.calculateButton.text")); // NOI18N
        calculateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calculateButtonActionPerformed(evt);
            }
        });

        factCurrentDayPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.factCurrentDayPanel.border.title"))); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel7, org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.jLabel7.text")); // NOI18N

        buyCurDayField.setText(org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.buyCurDayField.text")); // NOI18N
        buyCurDayField.setEnabled(false);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel8, org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.jLabel8.text")); // NOI18N

        sellCurDayField.setText(org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.sellCurDayField.text")); // NOI18N
        sellCurDayField.setEnabled(false);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel9, org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.jLabel9.text")); // NOI18N

        nacenkaCurDayField.setText(org.openide.util.NbBundle.getMessage(MotivationTopComponent.class, "MotivationTopComponent.nacenkaCurDayField.text")); // NOI18N
        nacenkaCurDayField.setEnabled(false);

        javax.swing.GroupLayout factCurrentDayPanelLayout = new javax.swing.GroupLayout(factCurrentDayPanel);
        factCurrentDayPanel.setLayout(factCurrentDayPanelLayout);
        factCurrentDayPanelLayout.setHorizontalGroup(
            factCurrentDayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(factCurrentDayPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(factCurrentDayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(factCurrentDayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(buyCurDayField)
                    .addComponent(sellCurDayField)
                    .addComponent(nacenkaCurDayField, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE))
                .addContainerGap())
        );
        factCurrentDayPanelLayout.setVerticalGroup(
            factCurrentDayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(factCurrentDayPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(factCurrentDayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buyCurDayField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(18, 18, 18)
                .addGroup(factCurrentDayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(sellCurDayField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(factCurrentDayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(nacenkaCurDayField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(headerLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(factCurrentDayPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(planPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(factPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addComponent(calculateButton)
                        .addGap(0, 12, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(headerLabel)
                .addGap(18, 18, 18)
                .addComponent(planPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(factPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(calculateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(55, 55, 55)))
                .addComponent(factCurrentDayPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_calculateButtonActionPerformed
        if (null == buyField.getText() || buyField.getText().isEmpty()) {
            UIUtils.showErrorMessage("Укажите сумму закупок");
            return;
        }
        if (null == sellField.getText() || sellField.getText().isEmpty()) {
            UIUtils.showErrorMessage("Укажите сумму продаж");
            return;
        }
        if (null == nacenkaField.getText() || nacenkaField.getText().isEmpty()) {
            UIUtils.showErrorMessage("Укажите процент наценки");
            return;
        }
        Double givenBuy = Double.valueOf(buyField.getText().replaceAll(",", "."));
        Double givenSell = Double.valueOf(sellField.getText().replaceAll(",", "."));
        Double givenNacenka = Double.valueOf(nacenkaField.getText().replaceAll(",", "."));
        if (null == planFact) {
            planFact = new PlanFact();
            planFact.setGenSell(givenSell);
            planFact.setGenPurchase(givenBuy);
            planFact.setGenNacenka(givenNacenka);
            planFact.setMonthId(Calendar.getInstance().get(Calendar.MONTH));
            planFact = CostDataProvider.getInstance().insertPlanFact(planFact);
        } else {
            planFact.setGenSell(givenSell);
            planFact.setGenPurchase(givenBuy);
            planFact.setGenNacenka(givenNacenka);
            CostDataProvider.getInstance().updatePlanFact(planFact);
        }
        calculate();
    }//GEN-LAST:event_calculateButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField buyCurDayField;
    private javax.swing.JTextField buyCurrentField;
    private javax.swing.JTextField buyField;
    private javax.swing.JButton calculateButton;
    private javax.swing.JPanel factCurrentDayPanel;
    private javax.swing.JPanel factPanel;
    private javax.swing.JLabel headerLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField nacenkaCurDayField;
    private javax.swing.JTextField nacenkaCurrentField;
    private javax.swing.JTextField nacenkaField;
    private javax.swing.JPanel planPanel;
    private javax.swing.JTextField sellCurDayField;
    private javax.swing.JTextField sellCurrentField;
    private javax.swing.JTextField sellField;
    // End of variables declaration//GEN-END:variables
    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    private void calculate() {
        Calendar start = Calendar.getInstance();
        start.set(Calendar.DAY_OF_MONTH, 1);

        Calendar end = Calendar.getInstance();
        end.set(Calendar.DAY_OF_MONTH, end.getActualMaximum(Calendar.DAY_OF_MONTH));

        Double buy = 0.0;
        Double sell = 0.0;
        Double nacenka = 0.0;
        List<Orders> orders = OrderDataProvider.getInstance().getOrdersByDate(start.getTime(), end.getTime());
        if (null != orders && !orders.isEmpty()) {
            for (Orders order : orders) {
                List<OrderPosition> orderPositions = OrderDataProvider.getInstance().getOrderPositionByOrderId(order.getId());
                if (null != orderPositions && !orderPositions.isEmpty()) {
                    for (OrderPosition op : orderPositions) {
                        buy += (op.getPurPrice() * op.getAmount());
                        sell += (op.getPrice() * op.getAmount());
                    }
                }
            }
        }

        Double diff = sell - buy;
        nacenka = diff / buy * 100;

        Double givenBuy = Double.valueOf(buyField.getText().replaceAll(",", "."));
        Double givenSell = Double.valueOf(sellField.getText().replaceAll(",", "."));
        Double givenNacenka = Double.valueOf(nacenkaField.getText().replaceAll(",", "."));

        Double percentBuy = buy / (givenBuy / 100);
        Double percentSell = sell / (givenSell / 100);
        buyCurrentField.setText(round(percentBuy, 2) + "%");
        sellCurrentField.setText(round(percentSell, 2) + "%");
        nacenkaCurrentField.setText(round(nacenka, 2) + "%");

        Double normaUpToCurDay = (givenBuy / Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH))
                * Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        Double buyCurrentDay = buy - normaUpToCurDay;
        Double normaSellUpToCurDay = (givenSell / Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH))
                * Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        Double sellCurrentDay = sell - normaSellUpToCurDay;

        buyCurrentField.setForeground(Color.BLACK);
        sellCurrentField.setForeground(Color.BLACK);
        nacenkaCurrentField.setForeground(Color.BLACK);

        buyCurDayField.setText("" + round(buyCurrentDay, 2));
        sellCurDayField.setText("" + round(sellCurrentDay, 2));
        nacenkaCurDayField.setText(round(nacenka, 2) + "%");
        buyCurDayField.setForeground(Color.BLACK);
        sellCurDayField.setForeground(Color.BLACK);
        nacenkaCurDayField.setForeground(Color.BLACK);

        if (buy > givenBuy) {
            buyCurrentField.setBackground(Color.GREEN);
        } else {
            buyCurrentField.setBackground(Color.RED);
        }
        if (sell > givenSell) {
            sellCurrentField.setBackground(Color.GREEN);
        } else {
            sellCurrentField.setBackground(Color.RED);
        }
        if (nacenka > givenNacenka) {
            nacenkaCurrentField.setBackground(Color.GREEN);
            nacenkaCurDayField.setBackground(Color.GREEN);
        } else {
            nacenkaCurrentField.setBackground(Color.RED);
            nacenkaCurDayField.setBackground(Color.RED);
        }

        if (buyCurrentDay >= 0.0) {
            buyCurDayField.setBackground(Color.GREEN);
        } else {
            buyCurDayField.setBackground(Color.RED);
        }
        if (sellCurrentDay >= 0.0) {
            sellCurDayField.setBackground(Color.GREEN);
        } else {
            sellCurDayField.setBackground(Color.RED);
        }
    }

    public double round(double value, int places) {
        long factor = (long) Math.pow(10, places);
        value = value * factor;
        long tmp = Math.round(value);
        return (double) tmp / factor;
    }
}
